package com.jzr.medical.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jzr.common.entity.vo.ResultPage;
import com.jzr.medical.db1.dto.FoodOrderDto;
import com.jzr.medical.db1.service.FoodOrderService;
import com.jzr.medical.db1.vo.FoodOrderDetailVo;
import com.jzr.medical.db1.vo.FoodOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/foodOrder")
public class FoodOrderController {
    @Autowired
    private FoodOrderService foodOrderService;

    @PostMapping("/list")
    public ResultPage<FoodOrderVo> list(@RequestBody FoodOrderDto record){
        IPage<FoodOrderVo> page = new Page<>(record.getPageNum(),record.getPageSize());
        List<FoodOrderVo> list = foodOrderService.list(page,record);

        if(list != null && !list.isEmpty()) {
            List<Integer> orderIds = list.stream().map(a->a.getId()).collect(Collectors.toList());
            List<FoodOrderDetailVo> listDetail = foodOrderService.listDetail(orderIds);
            if(listDetail != null && !listDetail.isEmpty()){
                for(FoodOrderVo orderVo : list){
                    orderVo.setDetails(
                            listDetail.stream().filter(a->a.getOrderId().equals(orderVo.getId())).collect(Collectors.toList())
                    );
                }
            }
        }
        page.setRecords(list);
        return ResultPage.success(page);
    }
//    @InitBinder
//    public void initBinder(WebDataBinder binder) {
//        //转换日期
//        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
//        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
//    }
}
